package com.atguigu.juc1205.demo;

import org.bouncycastle.jce.provider.BouncyCastleProvider;

import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @Author Joey
 * @Date 2021/9/26 14:20
 * @Description 多线程之间，顺序调用 A>B>C
 * 三个线程启动要求如下
 * AA 5次，BB 10次，CC 15次
 * 接着
 * AA 5次，BB 10次，CC 15次
 * 来10轮
 */
public class ConditionDemo {
    public static void main(String[] args) {
        KeyStore instance = null;
        Security.addProvider(new BouncyCastleProvider());

        try {
            instance = KeyStore.getInstance("PKCS12", "BC");
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        }
        System.out.println("---------------"+instance);
    }
    public static void main2(String[] args) {
        KeyStore instance = null;

        try {
            instance = KeyStore.getInstance("PKCS12", "BC");
        } catch (KeyStoreException e) {
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            e.printStackTrace();
        }
        System.out.println("---------------"+instance);

        ShareData shareData = new ShareData();
        
        new Thread(() -> {
            for (int i = 0; i < 10; i++){
                shareData.print(1);
//                shareData.print5();
            }
        },"AA").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++){
                shareData.print(2);
//                shareData.print10();
            }
        },"BB").start();

        new Thread(() -> {
            for (int i = 0; i < 10; i++){
                shareData.print(3);
//                shareData.print15();
            }
        },"CC").start();
        
    }
}

class ShareData {
    /**
     * A 1,B 2,C 3
     */
    private int num = 1;

    private Lock lock = new ReentrantLock();
    private Condition c1 = lock.newCondition();
    private Condition c2 = lock.newCondition();
    private Condition c3 = lock.newCondition();

    public void print(int sign){
        if (sign == 1){
            lock.lock();
            try {
                while (num != 1){
                    c1.await();
                }
                for (int i = 0; i < 5; i ++){
                    System.out.println(Thread.currentThread().getName() + " " + i);
                }
                num ++;
                c2.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }else if (sign == 2){
            lock.lock();
            try {
                while (num != 2){
                    c2.await();
                }
                for (int i = 0; i < 10; i ++){
                    System.out.println(Thread.currentThread().getName() + " " + i);
                }
                num ++;
                c3.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }else {
            lock.lock();
            try {
                while (num != 3){
                    c3.await();
                }
                for (int i = 0; i < 15; i ++){
                    System.out.println(Thread.currentThread().getName() + " " + i);
                }
                num = (num + 1) % 3;
                c1.signal();
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }

    public void print5() {
        lock.lock();
        try {
            // 判断
            while (num != 1) {
                c1.await();
            }
            // 干活
            for (int i = 0; i < 5; i++) {
                System.out.println("线程" + Thread.currentThread().getName() + "  " + i);
            }
            // 通知
            //1、变更标志位
            num = 2;
            // 通知第二个
            c2.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void print10() {
        lock.lock();
        try {
            // 判断
            while (num != 2) {
                c2.await();
            }
            // 干活
            for (int i = 0; i < 10; i++) {
                System.out.println("线程" + Thread.currentThread().getName() + "  " + i);
            }
            // 通知
            //1、变更标志位
            num = 3;
            // 通知第二个
            c3.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void print15() {
        lock.lock();
        try {
            // 判断
            while (num != 3) {
                c3.await();
            }
            // 干活
            for (int i = 0; i < 15; i++) {
                System.out.println("线程" + Thread.currentThread().getName() + "  " + i);
            }
            // 通知
            //1、变更标志位
            num = 1;
            // 通知第二个
            c1.signal();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

}